#include "common/config/config.h"
#include "common/log/log.h"
#include "common/more/time.h"

#include "issue_change.h"

namespace F2FB
{

namespace Database
{

std::vector<unsigned> IssueChange::ids(
    sqlite::database& database
)
{
    std::vector<unsigned> result;
    database << "SELECT id FROM issue_changes;"
        >> [&result](unsigned id)
    {
        result.emplace_back(id);
    };
    return result;
}

std::vector<std::shared_ptr<Entity>> IssueChange::entities(
    sqlite::database& database
)
{
    const auto sqlStr = "SELECT "
                        "  id "
                        ", issue_id "
                        ", diff "
                        ", user_id "
                        ", date_time "
                        "FROM issue_changes;";

    auto query = database << sqlStr;
    std::vector<std::shared_ptr<Entity>> result;
    query >>
        [&result](
            unsigned id,
            unsigned issueId,
            std::string diff,
            unsigned userId,
            std::string dateTime
        )
    {
        auto entity = std::make_shared<IssueChange>();
        entity->m_data["id"] = id;
        entity->m_data["issue_id"] = issueId;
        entity->m_data["diff"] = diff;
        entity->m_data["user_id"] = userId;
        entity->m_data["date_time"] = Common::stringToTime(dateTime, "%Y-%m-%d %H:%M:%S");
        result.push_back(entity);
    };
    return result;
}

} // namespace Database

} // namespace F2FB
